<template>
  <div class="metric-card">
    <div class="metric-icon">
      <el-icon>
        <Money v-if="icon === 'Money'" />
        <DataAnalysis v-else-if="icon === 'DataAnalysis'" />
        <Document v-else-if="icon === 'Document'" />
        <Calendar v-else-if="icon === 'Calendar'" />
        <ShoppingCart v-else-if="icon === 'ShoppingCart'" />
        <User v-else-if="icon === 'User'" />
        <TrendCharts v-else-if="icon === 'TrendCharts'" />
        <Wallet v-else-if="icon === 'Expenses' || icon === 'Income'" />
        <DataAnalysis v-else />
      </el-icon>
    </div>
    <div class="metric-content">
      <div class="metric-title">{{ title }}</div>
      <div class="metric-value">{{ value }}</div>
      <div v-if="growth !== undefined" class="metric-growth" :class="getGrowthClass">
        <el-icon><CaretTop v-if="showGrowthAsPositive" /><CaretBottom v-else /></el-icon>
        {{ Math.abs(growth) }}%
      </div>
      <div v-if="previousValue" class="metric-previous">
        <span>上期: {{ previousValue }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';
import { 
  Money, DataAnalysis, Document, Calendar, ShoppingCart, User, 
  TrendCharts, CaretTop, CaretBottom, Wallet
} from '@element-plus/icons-vue';

const props = defineProps({
  title: {
    type: String,
    required: true
  },
  value: {
    type: [String, Number],
    required: true
  },
  icon: {
    type: String,
    default: 'DataAnalysis'
  },
  growth: {
    type: Number,
    default: undefined
  },
  previousValue: {
    type: [String, Number],
    default: ''
  },
  inverseGrowth: {
    type: Boolean,
    default: false
  }
});

// 计算增长率是否显示为正向（绿色上箭头）
const showGrowthAsPositive = computed(() => {
  if (props.inverseGrowth) {
    // 对于成本类指标，增长为负是好的
    return props.growth < 0;
  }
  // 对于收入类指标，增长为正是好的
  return props.growth > 0;
});

// 计算增长率样式
const getGrowthClass = computed(() => {
  return {
    'positive': showGrowthAsPositive.value,
    'negative': !showGrowthAsPositive.value
  };
});
</script>

<style scoped>
.metric-card {
  background-color: #fff;
  border-radius: 4px;
  padding: 12px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
  display: flex;
  align-items: center;
  min-height: 70px;
  transition: all 0.3s;
}

.metric-card:hover {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.metric-icon {
  width: 36px;
  height: 36px;
  border-radius: 6px;
  background-color: #ecf5ff;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
  color: #409EFF;
  flex-shrink: 0;
}

.metric-icon .el-icon {
  font-size: 18px;
}

.metric-content {
  flex: 1;
}

.metric-title {
  font-size: 12px;
  color: #909399;
  margin-bottom: 4px;
  line-height: 1.2;
}

.metric-value {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  line-height: 1.2;
}

.metric-growth {
  font-size: 12px;
  display: flex;
  align-items: center;
  margin-top: 4px;
}

.metric-growth.positive {
  color: #67C23A;
}

.metric-growth.negative {
  color: #F56C6C;
}

.metric-previous {
  font-size: 12px;
  color: #909399;
  margin-top: 4px;
}

@media screen and (max-width: 768px) {
  .metric-card {
    margin-bottom: 10px;
  }
}
</style> 